package com.lykj.modules.shop.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.lykj.core.log.exception.ServiceException;
import com.lykj.core.redis.cache.LykjRedis;
import com.lykj.modules.shop.service.SMSService;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Objects;

@NoArgsConstructor
@Slf4j
@Service
public class SMSServiceImpl implements SMSService {

    @Autowired
    private LykjRedis lykjRedis;
    @Autowired
    private IAcsClient smsClient;


    private boolean verification(String templateCode,String number,String code){
        if(StringUtils.isBlank(code)){
            throw new ServiceException("请输入验证码");
        }
        String code1 = lykjRedis.get(templateCode + number);
        if(StringUtils.isBlank(code1)){
            throw new ServiceException("验证码错误");
        }
        boolean equals = Objects.equals(code, code1);
        if(equals){
            lykjRedis.del(templateCode + number);
            return true;
        }else{
            throw new ServiceException("验证码错误");
        }
    }


    private boolean sendSms(String title, String templateCode, String number, String code){
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(number);
        request.setSignName(短信签名名称);
        request.setTemplateCode(templateCode);
        request.setTemplateParam("{\"code\":"+code+"}");
        try {
            SendSmsResponse response = smsClient.getAcsResponse(request);
            boolean ok = "OK".equalsIgnoreCase(response.getCode());
            if(ok){
                log.info(title+"发送成功: " + JSON.toJSONString(response));
                lykjRedis.set(templateCode+number, code);
                return true;
            }else{
                log.error(title+"发送失败: " + JSON.toJSONString(response));
                throw new ServiceException(title+"发送失败");
            }
        } catch (ServerException e) {
            log.error(title+"发送失败:", e);
            throw new ServiceException(title+"发送失败");
        } catch (ClientException e) {
            log.error(title+"ErrCode:" + e.getErrCode());
            log.error(title+"ErrMsg:" + e.getErrMsg());
            log.error(title+"RequestId:" + e.getRequestId(),e);
            throw new ServiceException(title+"发送失败");
        }
    }


    /**
     * 注册时发送验证码
     *
     * @param mobilePhone
     */
    @Override
    public boolean sendRegisterVerifyCode(String mobilePhone) {
        return sendSms("注册验证码",注册验证码,mobilePhone, RandomStringUtils.random(6, chars));
    }

    /**
     * 验证注册验证码
     *
     * @param mobilePhone
     */
    @Override
    public boolean verificationRegisterVerifyCode(String mobilePhone, String code) {
        return verification(注册验证码,mobilePhone, code);
    }

    /**
     * 发送 重置密码验证码
     *
     * @param mobilePhone
     * @return
     */
    @Override
    public boolean sendVerifyCode(String mobilePhone) {
        return sendSms("重置密码验证码",重置密码验证码,mobilePhone, RandomStringUtils.random(6, chars));
    }

    /**
     * 验证 重置密码验证码
     *
     * @param mobilePhone
     * @return
     */
    @Override
    public boolean verificationVerifyCode(String mobilePhone, String code) {
        return verification(重置密码验证码,mobilePhone, code);
    }

    /**
     * 发送 变更重要信息验证码
     * @param mobilePhone
     * @return
     */
    @Override
    public boolean sendCustomerVerifyCode(String mobilePhone) {
        return sendSms("变更重要信息验证码",变更重要信息验证码,mobilePhone, RandomStringUtils.random(6, chars));
    }

    /**
     * 验证 变更重要信息验证码
     * @param mobilePhone
     * @return
     */
    @Override
    public boolean verificationCustomerVerifyCode(String mobilePhone, String code) {
        return verification(重置密码验证码,mobilePhone, code);
    }


}
